package edu.pku.sei.transformation.editor.controller.policies;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.requests.GroupRequest;

import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.transformation.editor.controller.commands.CommandFactory;

public class ReferenceNodeComponentEditPolicy extends NodeComponentEditPolicy {
	public static final String REQ_DELETE_REFERENCE = "deleteReference";
	public Command getCommand(Request request) {
		if (REQ_DELETE.equals(request.getType()))
			return getDeleteCommand((GroupRequest) request);
		else if (REQ_DELETE_REFERENCE.equals(request.getType())) {
			return getDeleteReferenceCommand();
		}
		else
			return super.getCommand(request); 
	}
	
	protected Command getDeleteReferenceCommand() {
		CompoundCommand cmd = new CompoundCommand();
		Object father = getHost().getParent().getModel();
		MetaNode node = (MetaNode) getHost().getModel();
		

		for (Object obj : node.getIncomings()) {
			if (obj instanceof MetaLink) {
				cmd.add(CommandFactory.getDeleteCommand((MetaLink) obj));
			}
		}
		for (Object obj : node.getOutgoings()) {
			if (obj instanceof MetaLink) {
				cmd.add(CommandFactory.getDeleteCommand((MetaLink) obj));
			}
		}
		cmd.add(CommandFactory.getDeleteCommand(node, father, true));
		return cmd;
	}
}
